const db = require('./db.js')

exports.getCommentByNewsId = async (newsid, query) => {
	try {
		let {_page = 1, _limit = 10 } = query
		if (_page < 1) {
			_page = 1
		}
		if (_limit < 1) {
			_limit = 1
		}
		if (_limit > 10) {
			_limit = 10
		}
		const _start = (_page - 1) * _limit
		const sqlStr = `select id,user_name,add_time,content from cms_news_comments where news_id=${newsid} order by id desc limit ${_start},${_limit}`
		return await db.query(sqlStr)
	} catch(err) {
		throw err
	}
}

exports.addComment = async (body) => {
	try {
		const sqlStr = `insert into cms_news_comments (news_id,content) values ('${body.news_id}', '${body.content}')`
		const ret = await db.query(sqlStr)
		return ret.insertId
	} catch (err) {
		throw err
	}
}

exports.getCommentById = async (id) => {
	try {
		const sqlStr = `select id,user_name,add_time,content from cms_news_comments where id='${id}'`
		const [comment] = await db.query(sqlStr)
		return comment
	} catch (err) {
		throw err
	}
}